<?php

namespace admin\controller;
use model\auth_rule;
class auth_group extends _base
{
    public function __construct()
    {
        parent::__construct();
        $this->assign('m','sys');
        $this->assign('y','auth_group');
    }

    /**
     * @Notes:列表
     * @Interface lists
     * @author: LZH
     * @Time: 2019/6/18   17:30
     */
    public function lists()
    {
        $aParams = array();
        //标题
        if ($title = I('title')) {
            $aParams['where']['title LIKE'] = "%{$title}%";
        }
        //筛选开始时间
        if(I('starttime') && I('starttime') !='1970-01-01 08:00:00'){
            $aParams['where']['creation_date >='] = strtotime(I('starttime'));
        }
        //筛选结束时间
        if(I('endtime') && I('endtime') !='1970-01-01 08:00:00'){
            $aParams['where']['creation_date <='] = strtotime(I('endtime'));
        }


        $aParams['page'] = max(1, intval(I('page')));
        $aParams['field'] = ['id','title','creation_date','status'];
        $aParams['order'] = 'id asc';
        $aData = M('auth_group')->page($aParams);

        $this->display('auth_group/auth_group_lists',['aData'=>$aData]);
    }

    /**
     * @Notes:添加
     * @Interface add
     * @author: LZH
     * @Time: 2019/6/18   17:26
     */
    public function add()
    {
        //表单提交操作
        if (I('act') == 'do') {
            $sSalt = $this->func_salt(6);//获取六位随机码
            $info = [
                'uniacid'=>uniacid(),
                'title'       => I('title'),
                'status' => I('status'),
                'creation_date' => time(),
            ];

            if ( M('auth_group')->getData(['field'=>['id'],'where'=>['title' => $info['title']]]))
                create_info('该角色名已存在', '', 'error');

            if (!$iId = M('auth_group')->add($info))
                create_info('角色添加失败', '', 'error');

            //操作记录待补充
            $this->log_act('添加角色 ID：' . (int)$iId);
            create_info('添加成功', gWebUrl('auth_group/lists'), 'success');

        }

        $this->display('auth_group/auth_group_add');
    }

    /**
     * @Notes:修改
     * @Interface edit
     * @author: LZH
     * @Time: 2019/6/18   17:31
     */
    public function edit()
    {
        //表单提交操作
        if (I('act') == 'do') {
            $info = array(
                'title'       => I('title'),
                'status' => I('status'),
                'last_update' => time(),
            );
            //where条件
            $aParams['where']['id'] = I('id');
            if ( M('auth_group')->getData(['field'=>['id'],'where'=>['title' => $info['title'],'id !=' =>I('id') ]]))
                create_info('该角色名已存在', '', 'error');


            if (!M('auth_group')->edit($info,$aParams))
                create_info('修改角色失败', '', 'error');

            //操作记录待补充
            $this->log_act('修改角色 ID：' . (int)I('id'));
            create_info('成功修改角色信息',gWebUrl('auth_group/lists'),'success');
        }

        $aParams['where'] = ['id' => I('id')];
        if (!$aData = M('auth_group')->getData($aParams))
            create_info('该角色不存在', '', 'error');

        $this->display('auth_group/auth_group_edit',['aData'=>$aData]);
    }

    /**
     * @Notes:修改状态
     * @Interface change
     * @author: LZH
     * @Time: 2019/6/18   17:34
     */
    public function change()
    {
        $iId = I('id');
        $iStatus = I('status');
        $aParams['where'] = array('id' => $iId);
        if (!$aData = M('auth_group')->getData($aParams))
            create_info('该角色不存在', '', 'error');
        if (!M('auth_group')->edit(['status' => $iStatus], $aParams))
            create_info('修改状态失败', '', 'error');

        $this->log_act('修改角色状态 ID：' . (int)$iId);
        create_info('修改状态成功', gWebUrl('auth_group/lists'), 'success');
    }

    /**
     * @Notes:删除
     * @Interface del
     * @author: LZH
     * @Time: 2019/6/18   17:33
     */
    public function del()
    {
        $iId = I('id');
        $aParams['where'] = array('id' => $iId);
        if (!$aData = M('auth_group')->getData($aParams))
            create_info('该角色不存在', '', 'error');
        if (!M('auth_group')->edit(['is_delete'=>2],$aParams))
            create_info('删除失败', '', 'error');

        $this->log_act('删除角色 ID：' . (int)$iId);
        create_info('删除成功', gWebUrl('auth_group/lists'), 'success');
    }

    public function del_all(){
        $iId = I('ids');
        $aParams['where'] =['id in'=>$iId];
        if (!M('auth_group')->edit(['is_delete'=>2],$aParams))
            create_info('删除失败', '', 'error');

        $this->log_act('删除角色 ID：' . $iId);
        create_info('删除成功', gWebUrl('auth_group/lists'), 'success');
    }

    /**
     * @Notes:给角色分配权限
     * @Interface rule_group_add
     * @author: LZH
     * @Time: 2019/6/19   10:08
     */
    public function rule_group_add(){
        global $_W,$_GPC;
        //表单提交操作
        if (I('act') == 'do') {
            if (!I('rules_id'))   create_info('请勾选权限','','error');

            if (!M('auth_group')->getData(['field' => 'title', 'where' => ['id' => I('id')]]))
                create_info('该数据不存在','','error');
            $aInfo = array(
                'rules'       => strip_tags(implode(',',I('rules_id'))),//把数组转换在字符串
                'last_update' => time(),
            );
            $aWhere['where'] = ['id' => (int)I('id')];
            if(!M('auth_group')->edit($aInfo,$aWhere))
                create_info('添加权限失败','','error');
            //操作记录待补充
            $this->log_act('角色分配权限 ID：'. (int)I('id'));
            create_info('添加权限成功',gWebUrl('auth_group/lists'),'success');
        }
        $aParams['field'] = ['id','title','rules'];;
        $aParams['where'] = ['id' => (int)I('id')];
        $group =  M('auth_group')->getData($aParams);
        $group['rules']=explode(',',$group['rules']);//把字符串打散为数组：
        $aData = (new auth_rule)->getTreeData('level','title');

        $this->display('auth_group/auth_rule_group_add',['aData'=>$aData,'group'=>$group]);
    }

   
}

